const express = require('express')
const userRouter = express.Router()

const userList = [{ username: 'admin', password: '123', age: 18, sex: '男' }]

/**
 * 注册
 */
userRouter.get('/register', (req, res) => {
	const username = req.query.username //姓名
	const password = req.query.password //密码
	const age = req.query.age //年龄
	const sex = req.query.sex //性别

	//添加用户列表
	userList.push({
		username,
		password,
		age,
		sex,
	})

	res.send({
		code: 1,
		message: '注册成功',
	})
})

/**
 * 登录
 */
userRouter.get('/login', (req, res) => {
	const username = req.query.username
	const password = req.query.password

	const user = userList.find(item => item.username == username && item.password == password)
	if (user) {
		res.send({
			code: 1,
			message: '登录成功',
		})
	} else {
		res.send({
			code: -1,
			message: '用户名或密码出错!',
		})
	}
})

module.exports = {userRouter}
